package com.test.action;

import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.hibernate.model.Person;
import com.hibernate.dao.PersonDAO;;


public class PersonAction extends ActionSupport
{
	private int id;
	
	private String username;
	
	private String password;
	
	private int age;
	
	public int getId()
	{
		return id;
	}
	
	public void setId(int id)
	{
		this.id = id;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}
	
	
	public String save() throws Exception
	{
		Person p = new Person();
		p.setUsername(this.username);
		p.setPassword(this.password);
		p.setRegisterDate(new java.util.Date());
		PersonDAO.save(p);
		
		List<Person> list = PersonDAO.listAll();
		HttpServletRequest requst 
			= ServletActionContext.getRequest();
		requst.setAttribute("list", list);
		
		return SUCCESS;
	}
	
	public String listAll()
	{
		List<Person> list = PersonDAO.listAll();
		HttpServletRequest requst 
			= ServletActionContext.getRequest();
		requst.setAttribute("list", list);
		
		return SUCCESS;
	}
	
	public String delete()
	{
//		HttpServletRequest request 
//			= ServletActionContext.getRequest();
//
//		String id = request.getAttribute("id").toString();
//		
		PersonDAO.delete(this.id);
		
		listAll();
		
		return SUCCESS;
	}
	
	public String getPerson() throws Exception
	{
		Person p = PersonDAO.get(this.id);
		
		HttpServletRequest request 
			= ServletActionContext.getRequest();
		request.setAttribute("person", p);
		
		return SUCCESS;
	}
}
